﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.DataStructures;
using DcsGauges.Gui.Rendering;
using DcsGauges.Gui.Gauges.Interfaces;
using DcsGauges.Shared.DataStructures;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;
using Microsoft.Xna.Framework;

namespace DcsGauges.Gui.Gauges
{
    class ThreeAnalogNumberGauge : GuiRenderableBase, IMultiValueGauge
    {
        private AnalogNumberGauge                   digit0Gauge                     = null;
        private AnalogNumberGauge                   digit1Gauge                     = null;
        private AnalogNumberGauge                   digit2Gauge                     = null;


        public ThreeAnalogNumberGauge(GuiRenderableBase parent, URect area, GraphicsDevice device,
            Tuple<DcsProperty, IGaugeValueConverter<float>> digit0, 
            Tuple<DcsProperty, IGaugeValueConverter<float>> digit1, 
            Tuple<DcsProperty, IGaugeValueConverter<float>> digit2) : base(parent, area)
        {
            this.digit0Gauge                        = new AnalogNumberGauge(this, device, new URect(new ULoc((1.0f / 3.0f) * 0.0f, 0.0f), new USize((1.0f / 3.0f), 1.0f)), digit0.Item1, digit0.Item2);
            this.digit1Gauge                        = new AnalogNumberGauge(this, device, new URect(new ULoc((1.0f / 3.0f) * 1.0f, 0.0f), new USize((1.0f / 3.0f), 1.0f)), digit1.Item1, digit1.Item2);
            this.digit2Gauge                        = new AnalogNumberGauge(this, device, new URect(new ULoc((1.0f / 3.0f) * 2.0f, 0.0f), new USize((1.0f / 3.0f), 1.0f)), digit2.Item1, digit2.Item2);
        }

        public Shared.DataStructures.DcsProperty[] Properties
        {
            get 
            {
                return new DcsProperty[]
                {
                    this.digit0Gauge.DataProperty,
                    this.digit1Gauge.DataProperty,
                    this.digit2Gauge.DataProperty,
                };
            }
        }

        public void SetValue(Shared.DataStructures.DcsProperty property, float value)
        {
            if (property == this.digit0Gauge.DataProperty)
            {
                this.digit0Gauge.Value = value;
            }
            else if (property == this.digit1Gauge.DataProperty)
            {
                this.digit1Gauge.Value = value;
            }
            else if (property == this.digit2Gauge.DataProperty)
            {
                this.digit2Gauge.Value = value;
            }
            else
            {
                Debug.Assert(false);
            }
        }

        public float GetValue(Shared.DataStructures.DcsProperty property)
        {
            if (property == this.digit0Gauge.DataProperty)
            {
                return this.digit0Gauge.Value;
            }
            else if (property == this.digit1Gauge.DataProperty)
            {
                return this.digit1Gauge.Value;
            }
            else if (property == this.digit2Gauge.DataProperty)
            {
                return this.digit2Gauge.Value;
            }
            else
            {
                Debug.Assert(false);
                return 0;
            }
        }

        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Microsoft.Xna.Framework.Rectangle parentRect, float globalZIndex)
        {
            Rectangle myRect = this.CalcViewportRect(parentRect);
            this.digit0Gauge.Render(spriteBatch, device, myRect, globalZIndex);
            this.digit1Gauge.Render(spriteBatch, device, myRect, globalZIndex);
            this.digit2Gauge.Render(spriteBatch, device, myRect, globalZIndex);
        }
    }
}
